package com.liuceng.zen.web.controller;

import com.liuceng.zen.Page;
import com.liuceng.zen.constants.Constants;
import com.liuceng.zen.pojo.domain.DiyForm;
import com.liuceng.zen.pojo.domain.Image;
import com.liuceng.zen.pojo.domain.LandingPage;
import com.liuceng.zen.pojo.form.DiyFormFrom;
import com.liuceng.zen.pojo.form.LandingPageForm;
import com.liuceng.zen.service.DiyFormService;
import com.liuceng.zen.service.ImageService;
import com.liuceng.zen.service.LandingPageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/forms")
public class FormBuilderController extends BaseController {
  @Autowired
  private DiyFormService diyFormService;
  @Autowired
  private ImageService imageService;
    @Autowired
    private LandingPageService landingPageService;
  // 列表显示
  @RequestMapping(value = "", method = RequestMethod.GET)
  public String list(@ModelAttribute("form") LandingPageForm form) {
      form.setMemberId(getCurrentUserId());
      form.setShowType(LandingPage.SHOW_TYPE);
     /* Map<String, Object> map = new HashMap<>();
      map.put("memberId", getCurrentUserId());
      map.put("showType",LandingPage.SHOW_TYPE);*/
      //List<LandingPage> landingPages =landingPageService.search(map);
      //Page<DiyForm> page=diyFormService.findPage(form);
      Page<LandingPage> page= landingPageService.findPage(form);
     /* int count=page.getTotalCount();
      count+=landingPages!=null?landingPages.size():0;*/
      //page.setTotalCount(count);
      form.setPage(page);
      form.setDiyFormDomain(landingPageService.getDiyFormDomain());
    return "forms/index";
  }

  @RequestMapping(value = "", method = RequestMethod.POST)
  public @ResponseBody Object doList(DiyFormFrom diyForm, HttpServletRequest req) {
    Integer memberId = getUserContext().getId();
    // List<DiyForm> diyForms = diyFormService.selectByMemberId(memberId);
    // diyForm.setSortInfo(req);
    diyForm.setMemberId(memberId);
    Page<DiyForm> page = diyFormService.findPage(diyForm);
    List<DiyForm> list = page.getContent();
    Map<String, Object> filterMap = new HashMap<String, Object>();
    filterMap.put("sEcho", 0);
    filterMap.put("iTotalRecords", page.getContent().size());// 实际的行数
    filterMap.put("iTotalDisplayRecords", page.getTotalCount());// 总行数
    filterMap.put("data", list);// 要以JSON格式返回
    return filterMap;
  }

  // 新增
  @RequestMapping(value = "/add", method = RequestMethod.GET)
  public String add(@ModelAttribute("form") DiyFormFrom diy, BindingResult result,
      HttpServletRequest req) {

    return "forms/add";
  }

  @RequestMapping(value = "/add", method = RequestMethod.POST)
  public String doAdd(@ModelAttribute("form") DiyFormFrom diy, BindingResult result,
      RedirectAttributes redirectAttributes, HttpServletRequest req) {
    DiyForm diyForm = new DiyForm();
    diyForm.setMemberId(getCurrentUserId());
    diyForm.setContent1(diy.getContent1());
    diyForm.setContent2(diy.getContent2());
    diyForm.setContent3(diy.getContent3());
    diyForm.setImageId1(diy.getImageId1());
    diyForm.setImageId2(diy.getImageId2());
    diyForm.setImageId3(diy.getImageId3());
    if (diy.getImageId1() != null) {
      Image image1 = imageService.find(diy.getImageId1());
      diyForm.setImagePath1(image1.getPath());
    }
    if (diy.getImageId2() != null) {
      Image image2 = imageService.find(diy.getImageId2());
      diyForm.setImagePath2(image2.getPath());
    }
    if (diy.getImageId3() != null) {
      Image image3 = imageService.find(diy.getImageId3());
      diyForm.setImagePath3(image3.getPath());
    }
    diyForm.setMainTitle(diy.getMainTitle());
    diyForm.setSubTitle(diy.getSubTitle());
    diyFormService.insert(diyForm);
    redirectAttributes.addFlashAttribute(Constants.OK_MSG, "营销表单新建成功");
    return "redirect:/forms";
  }

  // 列表显示
  @RequestMapping(value = "/list/ajax", method = RequestMethod.GET)
  public @ResponseBody Object formList(@ModelAttribute("form") DiyFormFrom form) {
    Map<String, Object> map = new HashMap<>();
    map.put("memberId", getCurrentUserId());
    List<DiyForm> diyForms = diyFormService.search(map);
    return diyForms;
  }

  //获取表单系统的落地页
  @RequestMapping(value="/list/landing_page/ajax",method=RequestMethod.GET)
  public @ResponseBody Object landingPageList(){
      Map<String, Object> map = new HashMap<>();
      map.put("memberId", getCurrentUserId());
      map.put("showType",LandingPage.SHOW_TYPE);
      map.put("status",LandingPage.STATUS);
      List<LandingPage> landingPages =landingPageService.search(map);
      return landingPages;
  }


  @RequestMapping(value = "/add/ajax", method = RequestMethod.POST)
  public @ResponseBody String add(@ModelAttribute("form") DiyFormFrom diy, BindingResult result,
      RedirectAttributes redirectAttributes, HttpServletRequest req) {
    DiyForm diyForm = new DiyForm();
    diyForm.setMemberId(getCurrentUserId());
    diyForm.setContent1(diy.getContent1());
    diyForm.setContent2(diy.getContent2());
    diyForm.setContent3(diy.getContent3());
    diyForm.setImageId1(diy.getImageId1());
    diyForm.setImageId2(diy.getImageId2());
    diyForm.setImageId3(diy.getImageId3());
    if (diy.getImageId1() != null) {
      Image image1 = imageService.find(diy.getImageId1());
      diyForm.setImagePath1(image1.getPath());
    }
    if (diy.getImageId2() != null) {
      Image image2 = imageService.find(diy.getImageId2());
      diyForm.setImagePath2(image2.getPath());
    }
    if (diy.getImageId3() != null) {
      Image image3 = imageService.find(diy.getImageId3());
      diyForm.setImagePath3(image3.getPath());
    }
    diyForm.setMainTitle(diy.getMainTitle());
    diyForm.setSubTitle(diy.getSubTitle());
    diyFormService.insert(diyForm);
    return "success";
  }

  // 表单详情
  @RequestMapping(value = "datail/{id}", method = RequestMethod.GET)
  public String datail(@PathVariable Integer id, @ModelAttribute("form") DiyFormFrom diy,
      BindingResult result, HttpServletRequest req) {
    DiyForm diyForm = diyFormService.find(id);
    diy.setDiyForm(diyForm);
    return "forms/detail";
  }
    // 表单详情
    @RequestMapping(value = "diy_form_datail_diy/{id}", method = RequestMethod.GET)
    public String diyFormDetail(@PathVariable Integer id, ModelMap modelMap) {
        LandingPage landingPage=landingPageService.find(id);
        modelMap.put("landingPage",landingPage);
        modelMap.put("diyFormDomain",landingPageService.getDiyFormDomain());
        return "forms/diy_form_detail";
    }
  // 修改
  @RequestMapping(value = "update/{id}", method = RequestMethod.GET)
  public String update(@PathVariable Integer id, @ModelAttribute("form") DiyFormFrom diy,
      BindingResult result, HttpServletRequest req) {
    DiyForm diyForm = diyFormService.find(id);
    req.setAttribute("diyForm", diyForm);
    return "forms/modify";
  }

  @RequestMapping(value = "update/{id}", method = RequestMethod.POST)
  public String doUpdate(@PathVariable Integer id, @ModelAttribute("form") DiyFormFrom diy,
      BindingResult result, RedirectAttributes redirectAttributes, HttpServletRequest req) {
    DiyForm diyForm = diyFormService.find(id);
    diyForm.setMemberId(getCurrentUserId());
    diyForm.setContent1(diy.getContent1());
    diyForm.setContent2(diy.getContent2());
    diyForm.setContent3(diy.getContent3());
    diyForm.setImageId1(diy.getImageId1());
    diyForm.setImageId2(diy.getImageId2());
    diyForm.setImageId3(diy.getImageId3());
    if (diy.getImageId1() != null) {
      Image image1 = imageService.find(diy.getImageId1());
      diyForm.setImagePath1(image1.getPath());
    }
    if (diy.getImageId2() != null) {
      Image image2 = imageService.find(diy.getImageId2());
      diyForm.setImagePath2(image2.getPath());
    }
    if (diy.getImageId3() != null) {
      Image image3 = imageService.find(diy.getImageId3());
      diyForm.setImagePath3(image3.getPath());
    }
    diyForm.setMainTitle(diy.getMainTitle());
    diyForm.setSubTitle(diy.getSubTitle());
    diyFormService.update(diyForm);
    redirectAttributes.addFlashAttribute(Constants.OK_MSG, "落地页修改成功");
    return "redirect:/forms";
  }

  // 删除
  @RequestMapping("/delete/{id}")
  public String delete(@PathVariable Integer id, RedirectAttributes redirectAttributes) {
    diyFormService.delete(id);
    redirectAttributes.addFlashAttribute(Constants.OK_MSG, "落地页删除成功");
    return "redirect:/forms";
  }
}
